import { app, globalShortcut } from "electron";
import { InitEvent } from "./event";
import { closeAllWindows, createWindow } from "./window/CreateWindow.ts";
import { fileURLToPath } from "node:url";
import path from "node:path";
import { InitDb } from "./db/index.ts";
import betterSqlite3Type from "better-sqlite3";
export const userDataPath = app.getPath("userData");
export const dataBsePath = path.join(userDataPath, "Nexa.db");
export const __dirname = path.dirname(fileURLToPath(import.meta.url));
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = "true";
process.env.APP_ROOT = path.join(__dirname, "..");
export const APP_ROOT = process.env.APP_ROOT;
export const VITE_DEV_SERVER_URL = process.env.VITE_DEV_SERVER_URL;
export const MAIN_DIST = path.join(APP_ROOT, "dist-electron");
export const RENDERER_DIST = path.join(APP_ROOT, "dist");
process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL ? path.join(APP_ROOT, "public") : RENDERER_DIST;
export const publicPath = process.env.VITE_PUBLIC || "default/public/path";
export let Db: betterSqlite3Type.Database = InitDb(dataBsePath);

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

app.on("quit", () => {
  globalShortcut.unregisterAll();
  closeAllWindows();
  Db.close();
  console.log("App is quitting...");
});

app
  .whenReady()
  .then(async () => {
    console.log("数据库地址为", dataBsePath);
    InitEvent();
    createWindow();
  })
  .catch((err) => console.error("Error during app initialization", err));
